package com.zq.springadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zq.springadmin.dao.entity.*;
import com.zq.springadmin.mapper.UserMapper;
import com.zq.springadmin.service.IPermissionService;
import com.zq.springadmin.service.IRolePermissionService;
import com.zq.springadmin.service.IRoleService;
import com.zq.springadmin.service.IUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class MyUserDetailServerImpl implements UserDetailsService {
    @Autowired
    UserMapper userService;


        //    用户角色表
    @Autowired
    private IUserRoleService sysUserRoleService;
    //角色权限表
    @Autowired
    private IRolePermissionService sysRoleMenuService;
    //    权限表
    @Autowired
    private IPermissionService sysMenuService;
//    角色表
    @Autowired
    private IRoleService sysRoleService;

@Autowired
private RedisTemplate<String,String> redisTemplate;



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.selectOne(new LambdaQueryWrapper<User>().
                eq(StringUtils.hasText(username), User::getUsername, username));
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
 
        MyTUserDetail myTUserDetail=new MyTUserDetail();
        myTUserDetail.setUsers(user);


        // 查询用户权限



        List<UserRole> sysUserRoles = sysUserRoleService.list(new LambdaQueryWrapper<UserRole>().
                eq(UserRole::getUserId, user.getId()));
//        得到角色id集合
        List<Integer> roleIds = sysUserRoles.stream().map(UserRole::getRoleId).toList();
        if (roleIds.size()== 0){
          myTUserDetail.setRoles(new ArrayList<>());
            myTUserDetail.setPermissions(new ArrayList<>());
        }
        List<String> listRole=new ArrayList<>();
        roleIds.forEach(roleId->{
            Role role = sysRoleService.getById(roleId);
            listRole.add(role.getName());
        });

        // 拥有的角色列表
        myTUserDetail.setRoles(listRole);

        List<String> listPermission=new ArrayList<>();
//    根据角色id查权限id
        roleIds.forEach(roleId -> {
            List<RolePermission> roleMenus = sysRoleMenuService.list(new LambdaQueryWrapper<RolePermission>().
                    eq(RolePermission::getRoleId, roleId));
            List<Integer> mends = roleMenus.stream().map(RolePermission::getPermissionId).toList();
            mends.forEach(menuId ->{
                Permission sysMenu = sysMenuService.getById(menuId);
                listPermission.add(sysMenu.getName());
            });
        });
//     将查询到的权限放到redis中，便于下次的直接使用

        myTUserDetail.setPermissions( listPermission);
        return myTUserDetail;
    }
}